import {app, BrowserWindow, Menu, Tray, nativeImage} from "electron";
import path from "node:path";

export function setMainWindowOptions(win: EWindow) {
    // 设置最小窗体大小
    win.setMinimumSize(670, 400)
// 设置托盘菜单以及托盘图标、事件等
    const trayIcon = nativeImage.createFromPath(path.join(process.env.DIST, './icon/pika.ico'))
    const tray = new Tray(trayIcon)
    // 设置托盘菜单
    const contextMenu = Menu.buildFromTemplate([
        {label: '显示窗口', click: () => win && win.show()},
        {label: '最小化窗口', click: () => win && win.minimize()},
        {type: 'separator'},
        {label: '退出应用', click: () => app.quit()},
    ]);
    tray.setContextMenu(contextMenu);
    // 设置托盘图标双击显示
    tray.on('double-click', () => {
        if (!win.isVisible()) {
            win.setSkipTaskbar(false)
            win.show()
        }
    })
    if (import.meta.env.MODE === 'development' && process.env.DEV_BASE_URL) {
        win.loadURL(process.env.DEV_BASE_URL)
    } else {
        // win.loadFile('dist/index.html')
        win.loadFile(path.join(process.env.DIST, './index.html'))
        console.log(process.env.DIST)
    }

    // 取消任务栏微缩窗口的默认事件，改为最小化到托盘
    // win.on('close', (e) => {
    //     e.preventDefault()
    //     win.setSkipTaskbar(true)
    //     win.minimize()
    // })
}

export function createWindow(options: EWindowOption) {
    return new BrowserWindow(options) as EWindow
}